package com.linkage.educloud.ah.adapter.dynamic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.DynamicImageBean;
import com.linkage.educloud.ah.util.ImageLoader;

import java.util.List;

/**
 * Created by Mr'Tang on 2018/9/18.
 */

public class DynamicImageListAdapter extends android.widget.BaseAdapter {
    private List<DynamicImageBean> images;
    private Context mcontext;

    public DynamicImageListAdapter(Context context, List<DynamicImageBean> images) {
        this.mcontext = context;
        this.images = images;
    }

    @Override
    public int getCount() {
        if (images.size() > 9) {
            return 9;
        }
        return images.size();
    }

    @Override
    public DynamicImageBean getItem(int position) {
        // TODO Auto-generated method stub
        return images.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView cs_image;
        if (convertView == null) {
            convertView = LayoutInflater.from(mcontext).inflate(
                    R.layout.item_dynamic_image, null);
            cs_image = (ImageView) convertView
                    .findViewById(R.id.cs_image);
            convertView.setTag(cs_image);
        } else {
            cs_image = (ImageView) convertView.getTag();
        }
        final DynamicImageBean bean = getItem(position);
        if (bean.getImage().startsWith("http")) {
            ImageLoader.loadAds(mcontext, bean.getImage(), cs_image);
        } else {
            if (bean.getType() == 4) {
                ImageLoader.loadAds(mcontext, Constants.IMAGE_IP_hexiaoxiao + bean.getImage(), cs_image);
            } else {
                ImageLoader.loadAds(mcontext, Constants.IP + "/educloud_share/" + bean.getImage(), cs_image);
            }
        }
        return convertView;
    }
}

